package com.xunfang.portal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xunfang.common.RedisConst;
import com.xunfang.portal.mapper.BookMapper;
import com.xunfang.portal.model.entity.Book;
import com.xunfang.portal.model.vo.CartItemVo;
import com.xunfang.portal.model.vo.CartVo;
import com.xunfang.portal.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author yellow Doctor
 * @date 2025-10-16
 * @desc https://blog.csdn.net/hekai7217
 */

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate<String, CartVo> redisTemplate;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public CartVo getCart(Long userId) {
        //从Redis中获取用户的购物车
        ValueOperations<String, CartVo> ops = redisTemplate.opsForValue();
        CartVo cart = ops.get(RedisConst.USER_CART_PREFIX + ":" + userId);

        if(cart == null){
            cart = new CartVo();
            cart.setUserId(userId);
        }
        ops.set(RedisConst.USER_CART_PREFIX + ":" + userId, cart, 3,TimeUnit.DAYS);

        return cart;
    }

    @Override
    public CartVo addCartItem(Long userId, Long bookId) {

        //拿到 购物车
        CartVo cart = getCart(userId);

        //通过bookId 拿到book  转成 CartItemVo
        Book book = bookMapper.selectById(bookId);
        //不要使用 copy (和 book 字段 不一致容易出现 null这样的 null值 购物车中一推null)
        CartItemVo cartItemVo = new CartItemVo(userId,
                bookId,
                book.getName(),
                book.getImgSrc(),
                book.getPrice(), 1); //默认给 购买了一本书

        //添加到购物车中
        cart.addItem(cartItemVo);

        //添加到redis中
        redisTemplate.opsForValue()
                .set(RedisConst.USER_CART_PREFIX + ":" + userId,
                        cart,
                        3,
                        TimeUnit.DAYS);

        return cart;
    }

    @Override
    public CartVo removeCartItem(Long userId, Long bookId) {

        CartVo cart = getCart(userId);
        cart.removeItem(bookId);
        //更新redis中的购物车
        redisTemplate.opsForValue()
                .set(RedisConst.USER_CART_PREFIX + ":" + userId,
                        cart,
                        3,
                        TimeUnit.DAYS);
        return cart;
    }

    /**
     *    购物车中商品数量的修改
     * @param userId
     * @param bookId
     * @param count
     * @return
     */
    @Override
    public CartVo updateCartItemCount(Long userId, Long bookId, Integer count) {

        //拿到购物车
        CartVo cart = getCart(userId);

        cart.updateItemQuantity(bookId,count);

        //更新redis中的购物车
        redisTemplate.opsForValue()
                .set(RedisConst.USER_CART_PREFIX + ":" + userId,
                        cart,
                        3,
                        TimeUnit.DAYS);

        return cart;
    }

    @Override
    public CartVo clearCartItem(Long userId) {
        //拿到购物车
        CartVo cart = getCart(userId);
        cart.clearCart();

        //更新redis中的购物车
        redisTemplate.opsForValue()
                .set(RedisConst.USER_CART_PREFIX + ":" + userId,
                        cart,
                        3,
                        TimeUnit.DAYS);
        return cart;
    }
}
